package com.example.shiro.demo.redis;

/**
 * <Description> <br>
 *
 * @author renweiping<br>
 * @taskId:   <br>
 * @version 1.0<br>
 * @createDate 2019/10/14 17:52 <br>
 * @see com.example.shiro.demo <br>
 */
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;


import java.util.HashSet;
import java.util.Set;

@Component
public class RedisUtil {

    /**
     * Default value of count.
     */
    protected static final int DEFAULT_COUNT = 100;

    /**
     * The number of elements returned at every iteration.
     */
    private int count = DEFAULT_COUNT;


    @Autowired
    JedisPool jedisPool;

    private Jedis getResource(){
        return jedisPool.getResource();
    }

    public void set(byte[] key, byte[] value) {
        Jedis jedis =getResource();
        try{
            jedis.set(key,value);

        }finally {
            if (jedis!=null){
                jedis.close();
            }
        }
    }

    public void setex(String key,String value ,int time ) {
        Jedis jedis =getResource();
        try{
            jedis.setex(key,time,value);

        }finally {
            if (jedis!=null){
                jedis.close();
            }
        }
    }


    public void setex(byte[] key,byte[] value ,int time ) {
        Jedis jedis =getResource();
        try{
            jedis.setex(key,time,value);
        }finally {
            if (jedis!=null){
                jedis.close();
            }
        }
    }


    /**
     * Return all the keys of Redis db. Filtered by pattern.
     * @param pattern key pattern
     * @return key set
     */
    public Set<byte[]> keys(byte[] pattern) {
        Set<byte[]> keys = new HashSet<byte[]>();
        Jedis jedis = getResource();

        try {
            ScanParams params = new ScanParams();
            params.count(count);
            params.match(pattern);
            byte[] cursor = ScanParams.SCAN_POINTER_START_BINARY;
            ScanResult<byte[]> scanResult;
            do {
                scanResult = jedis.scan(cursor, params);
                keys.addAll(scanResult.getResult());
                cursor = scanResult.getCursorAsBytes();
            } while (scanResult.getStringCursor().compareTo(ScanParams.SCAN_POINTER_START) > 0);
        } finally {
            jedis.close();
        }
        return keys;

    }

    public void expire(byte[] key, int i){
        Jedis jedis = getResource();
        try{
            jedis.expire(key,i);
        }finally {
            if (jedis!=null){
                jedis.close();
            }
        }
    }

    public byte[] get(byte[] key) {
        Jedis jedis = getResource();
        try{
            return jedis.get(key);
        }finally {
            if (jedis!=null){
                jedis.close();
            }
        }
    }

    public void del(byte[] key) {
        Jedis jedis = getResource();
        try{
            jedis.del(key);
        }finally {
            if (jedis!=null){
                jedis.close();
            }
        }
    }

    public Set<byte[]> getKeys(String shiro_sessiom_prefix) {
        Jedis jedis = getResource();
        try{
            return jedis.keys((shiro_sessiom_prefix+"*").getBytes());
        }finally {
            if (jedis!=null){
                jedis.close();
            }
        }
    }
}
